package com.su.dolphin.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.su.dolphin.utils.ViewHolderUtil;

/**
 * @param <T>
 * @className: KKBaseAdapter
 * @description: 对Adapte进行封装, 用来处理显示单一 item的 listview gridview等
 * @author: gaoshuai
 * @date: 2015-7-10 上午10:21:51
 */
public abstract class DpBaseAdapter<T> extends BaseAdapter {
    protected List<T> mDataList;
    private Context mContext;

    public DpBaseAdapter(Context context) {
        mContext = context;
    }

    public Context getContext() {
        return mContext;
    }

    public void clear(){
        if (mDataList != null){
            mDataList.clear();
        }
    }

    public void setData(List<T> listData) {
        mDataList = listData;
        notifyDataSetChanged();
    }
    public void addData(List<T> listData) {
        if (mDataList != null){
            mDataList.addAll(listData);
        }else{
            mDataList = listData;
        }
        notifyDataSetChanged();
    }
    public T getData(int position) {
        return mDataList.get(position);
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public T getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ViewHolderUtil.inflateView(onGetItemLayout(), parent);
        }
        onGetView(convertView, position);
        return convertView;
    }

    /**
     * @return
     * @title: onGetItemLayout
     * @description:设置item 的layout
     * @return: int
     */
    public abstract int onGetItemLayout();

    /**
     * @param convertView
     * @param position
     * @title: onGetView
     * @description: 类似于getView, 不用考虑 View复用, ViewHolder等问题了.
     * @return: void
     */
    public abstract void onGetView(View convertView, int position);

    /**
     * @param convertView
     * @param resId
     * @return
     * @title: findViewById
     * @description: 检索convertView中缓存的View
     * @return: View
     */
    public <T extends View> T findViewById(View convertView, int resId) {
        return ViewHolderUtil.findViewById(convertView, resId);
    }

}
